<template>
	<view>
		<view class="margin-sm">
			<uni-forms ref="submitForm" :modelValue="from" :rules="rules">
				<uni-forms-item label="客户信息" labelWidth="80px" style="display: flex;align-items: center;"
					class="bg-white  radius padding-xs">
					<view class=" ">
						{{customer.student.name}}&nbsp;&nbsp;&nbsp;&nbsp;{{customer.parentsInfo.phone}}
					</view>
				</uni-forms-item>
				<uni-forms-item label="课包信息" labelWidth="80px" required name="curriculum"
					class="bg-white  radius padding-xs">
					<uni-data-select v-model="from.curriculum" :localdata="curriculumList"
						@change="xuanzekebao"></uni-data-select>
				</uni-forms-item>
				<view style="display: flex;" class="bg-white  radius padding-xs">

					<uni-forms-item label="赠课节数" labelWidth="80px" style="flex: 1;margin-bottom: 0px;">
						<uni-easyinput v-model="from.numberComplimentaryCourses" :clearable="false" type="number">
							<view v-slot:right>122</view>
						</uni-easyinput>
					</uni-forms-item>


					<uni-forms-item label="优惠金额" labelWidth="80px" style="flex: 1;margin-left: 20rpx;">
						<uni-easyinput v-model="from.discountPrice" :clearable="false" type="number"></uni-easyinput>
					</uni-forms-item>

				</view>




				<view style="display: flex;margin-bottom: 30rpx;" class="bg-white radius padding-xs margin-top-sm">
					<view style="flex: 1;">
						<view class="juzhong">{{curriculum.price}}</view>
						<view class="juzhong font-11 margin-top-sm">课包价格</view>
					</view>
					<view style="flex: 1;" class="juzhong font-23">-</view>
					<view style="flex: 1;">
						<view class="juzhong">{{from.discountPrice}}</view>
						<view class="juzhong font-11 margin-top-sm">优惠金额</view>
					</view>
					<view style="flex: 1;" class="juzhong font-23">=</view>
					<view style="flex: 1;">
						<view class="juzhong">{{curriculum.price-from.discountPrice}}</view>
						<view class="juzhong font-11 margin-top-sm">付款金额</view>
					</view>
				</view>


				<view style="display: flex;margin-bottom: 30rpx;" class="bg-white radius padding-xs">
					<view style="flex: 1;">
						<view class="juzhong">{{curriculum.number}}</view>
						<view class="juzhong font-11 margin-top-sm">课包课时</view>
					</view>
					<view style="flex: 1;" class="juzhong font-23">+</view>
					<view style="flex: 1;">
						<view class="juzhong">{{from.numberComplimentaryCourses}}</view>
						<view class="juzhong font-11 margin-top-sm">赠送课时</view>
					</view>
					<view style="flex: 1;" class="juzhong font-23">=</view>
					<view style="flex: 1;">
						<view class="juzhong">{{parseInt(curriculum.number)+parseInt(from.numberComplimentaryCourses)}}
						</view>
						<view class="juzhong font-11 margin-top-sm">实得课时</view>
					</view>
				</view>
				<uni-forms-item label="报名类型" labelWidth="80px" required name="typeBx"
					class="bg-white  radius padding-xs">
					<uni-data-checkbox style="width: 100%;" mode="button" v-model="from.typeBx"
						:localdata="typeBxList"></uni-data-checkbox>
				</uni-forms-item>

				<uni-forms-item label="收款类型" labelWidth="80px" required name="radio"
					class="bg-white  radius padding-xs">
					<uni-data-checkbox style="width: 100%;" mode="button" v-model="from.radio"
						:localdata="radioList"></uni-data-checkbox>
				</uni-forms-item>

				<uni-forms-item label="业绩归属" labelWidth="80px" required class="bg-white  radius padding-xs">
					<view v-for="(item,index) in from.achievementAttributionList"
						style="display: flex;margin-bottom: 20rpx;">
						<view style="flex: 2;">
							<uni-data-select v-model="item.axUid" :localdata="createUserNameList"></uni-data-select>
						</view>
						<view style="flex: 2;margin-left: 20rpx;">
							<uni-easyinput v-model="item.num" :clearable="false" type="number">%</uni-easyinput>
						</view>
						<view style="flex: 1;margin-left: 20rpx;">
							<uni-icons type="trash-filled" size="24" color="#e43d33"
								@click="from.achievementAttributionList.splice(index, 1)"></uni-icons>
						</view>
					</view>

					<view v-if="dataCheck()!=null" class="font-11" style="color: #dd524d;">{{dataCheck()}}</view>
					<button @click="achievementAttributionListAdd"
						class="login-btn cu-btn  bg-orange light ">加一个</button>
				</uni-forms-item>

				<uni-forms-item label="合同编号" labelWidth="80px" name="offlineContractId" required
					class="bg-white  radius padding-xs">
					<uni-easyinput :clearable="false" v-model="from.offlineContractId"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item label="备注" labelWidth="80px" class="bg-white  radius padding-xs">
					<uni-easyinput :clearable="false" v-model="from.remarks"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<button @click="submitForm" class="login-btn cu-btn block bg-blue lg round">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getById
	} from '@/api/customer'
	import {
		getcommon
	} from '@/utils/common'
	import {
		doInsert
	} from '@/api/approval/record/ApprovalRecordManagementApi.js'
	export default {
		data() {
			return {
				customer: {
					parentsInfo: {

					},
					student: {

					}
				},
				from: {
					customerId: null,
					numberComplimentaryCourses: 0,
					discountPrice: 0,
					achievementAttributionList: []
				},
				typeBxList: [{
						text: '新签',
						value: 0
					},
					{
						text: '续费',
						value: 1
					}
				],
				radioList: [{
						text: '定金',
						value: 0
					},
					{
						text: '全款',
						value: 1
					}
				],
				curriculumList: [],
				curriculum: {
					price: 0,
					number: 0
				},
				// 校验规则
				rules: {
					typeBx: {
						rules: [{
							required: true,
							errorMessage: '报名类型不能为空'
						}]
					},
					radio: {
						rules: [{
							required: true,
							errorMessage: '收款类型不能为空'
						}]
					},
					curriculum: {
						rules: [{
							required: true,
							errorMessage: '课包不能为空'
						}]
					},
					offlineContractId: {
						rules: [{
							required: true,
							errorMessage: '合同编号不能为空'
						}]
					},

				},
			}
		},
		onLoad(o) {
			if (o.customerId != null) {
				this.customer.id = o.customerId
				this.from.customerId = o.customerId
			}
			this.init()
		},
		methods: {
			init() {
				this.curriculumList = getcommon('curriculumList')
				for (let s of this.curriculumList) {
					s.text = s.number + '课时, ' + s.price + '元 ,' + s.title
					s.value = s.id
				}

				this.createUserNameList = getcommon('createUserNameList')
				for (let s of this.createUserNameList) {
					s.text = s.realName
					s.value = s.id
				}
				this.from.offlineContractId = this.getofflineContractId()
				this.achievementAttributionListAdd()
				//查询用户信息
				this.getUserinfo()
			},
			achievementAttributionListAdd() {
				let num = 100
				if (this.from.achievementAttributionList.length == 0) {
					num = 100
				}
				if (this.from.achievementAttributionList.length == 1) {
					num = 50
				}
				this.from.achievementAttributionList.push({
					axUid: null,
					num: num,
				})
			},
			//选择课包
			xuanzekebao() {
				for (let s of this.curriculumList) {
					if (this.from.curriculum == s.id) {
						this.curriculum = s
					}
				}

			},
			async getUserinfo() {
				let {
					data
				} = await getById({
					id: this.customer.id
				})
				this.customer = data
			},

			dataCheck() {
				let hasEmptyID = this.from.achievementAttributionList.some(item => item.axUid === null || item.axUid ===
					undefined || item.axUid === '');
				if (hasEmptyID) {
					return "业绩归属 的用户不能是空的，如果不需要请删除"
				}


				if (this.from.achievementAttributionList.reduce((acc, item) => parseInt(acc) + parseInt(item.num), 0) !=
					100) {
					return "业绩归属 的值加起来必须是100"
				}
				return null
			},
			getofflineContractId() {

				var date = new Date();
				if ((date.getMonth() + 1) < 10) {
					var aaa = "0" + (date.getMonth() + 1)
				} else {
					var aaa = (date.getMonth() + 1)
				}
				return "QYL_" + date.getFullYear() + aaa + date.getDate() + date.getHours() + date.getMinutes() + date
					.getSeconds() + date.getMilliseconds()
			},
			submitForm() {
				if (this.dataCheck() != null) {
					uni.showModal({
						title: "数据校验失败",
						content: "请检查数据"
					})
					return
				}
				this.$refs['submitForm'].validate().then(async res => {

					let asdf = JSON.parse(JSON.stringify(this.from))

					let achievementAttributionList = []
					for (let s of asdf.achievementAttributionList) {
						achievementAttributionList.push({
							"userinfo": {
								"realName": s.axUid
							},
							"num": s.num,
							"user": {
								"id": s.axUid,
								"realName": s.axUid,
								"userName": s.axUid
							}
						})
					}

					asdf.achievementAttributionList = JSON.stringify(achievementAttributionList)

					asdf.curriculum = {
						id: asdf.curriculum
					}
					let {
						success
					} = await doInsert(asdf)

				})

			}

		}
	}
</script>

<style>

</style>